home *** CD-ROM | disk | FTP | other *** search
/ Software Vault: The Gold Collection / Software Vault - The Gold Collection (American Databankers) (1993).ISO / cdr50 / wbb13.zip / SAMPLEB1.BAS < prev    next >
BASIC Source File  |  1993-06-04  |  5KB  |  239 lines

  1.  
  2.  
  3. rem
  4. rem This program creates and utilizes buttons in BasicBasic.  While the
  5. rem buttons it creates are used for an unusual purpose, the programming
  6. rem methods shown will be useful to any button program.  This program
  7. rem works the same under DOS or Windows.
  8. rem
  9.  
  10.  
  11.     rem
  12.     rem  make windows window my size and give it a name
  13.     rem
  14.  
  15.     rem windows size 29,9,45,15
  16.     rem windows name "Button Move"
  17.  
  18.     cls
  19.  
  20.     rem
  21.     rem if no mouse this doesn't run
  22.     rem
  23.     a=mouseon
  24.     if a<>-1 then
  25.       beep
  26.       print "Mouse required to run this program."
  27.       stop
  28.     end if
  29.  
  30.  
  31.     rem
  32.     rem some general inits
  33.     rem
  34.     buttonfore=7
  35.     buttonback=4
  36.     borderfore=7
  37.     borderback=1 
  38.     boardtop=10
  39.     boardleft=30
  40.     topy=3
  41.     topx=3
  42.     maxbuttons=8
  43.     totalx= (boardleft+(topx+2)*3)-(boardleft-1)+1
  44.  
  45.     dim board(topx+1,topy+1)   : rem keeps pointer to text for button
  46.     dim boardc(topx+1,topy+1)  : rem keeps button foreground color
  47.     dim text$(maxbuttons)      : rem keeps text
  48.  
  49.     rem
  50.     rem define text
  51.     rem
  52.     text$(0)="Bas"
  53.     text$(1)="ic "
  54.     text$(2)="is "
  55.     text$(3)="you"
  56.     text$(4)="r b"
  57.     text$(5)="est"
  58.     text$(6)="val"
  59.     text$(7)="ue "
  60.  
  61.  
  62.  
  63.  
  64.       rem
  65.       rem flag border as occupied
  66.       rem
  67.  
  68.       for x=0 to topx+1
  69.     board(x,0)=1
  70.     board(x,topy+1)=1
  71.       next x
  72.       for y=0 to topy+1
  73.     board(0,y)=1
  74.     board(topx+1,y)=1
  75.       next y
  76.  
  77.  
  78.       rem
  79.       rem place my buttons on board
  80.       rem
  81.  
  82.          rem
  83.          rem first skip randomly into random number generator using
  84.          rem  seconds of clock
  85.          rem
  86.          t$=time$
  87.          t=val(right$(t$,2))
  88.          for i=1 to t
  89.              x=rnd
  90.          next i
  91.  
  92.       for i=59 to 66
  93.         place10:
  94.               x=int(rnd*3)+1
  95.               y=int(rnd*3)+1
  96.               if board(x,y)<>0 then goto place10
  97.               board(x,y)=i
  98.       next i
  99.  
  100.  
  101.       rem
  102.       rem place button colors
  103.       rem
  104.       boardc(1,1)=0
  105.       boardc(1,2)=4
  106.       boardc(1,3)=5
  107.       boardc(2,1)=6
  108.       boardc(2,2)=0
  109.       boardc(2,3)=4
  110.       boardc(3,1)=5
  111.       boardc(3,2)=6
  112.       boardc(3,3)=0
  113.  
  114.  
  115.     rem
  116.     rem
  117.     rem  color background
  118.     rem
  119.     color borderfore,borderback
  120.       for y=boardtop-1 to boardtop+topy+2
  121.     locate y,boardleft-1,0
  122.     print space$(totalx);
  123.       next y
  124.  
  125.  
  126.     rem
  127.     rem display exit and help buttons
  128.     rem
  129.       CBUTTON "Exit",1068,0,"Push",0,boardleft+(topy*3)+1,boardtop-1,6,1,buttonfore,4
  130.       CBUTTON "Help",1067,0,"Push",0,boardleft-1,boardtop-1,6,1,buttonfore,4
  131.  
  132.  
  133.     rem
  134.     rem display buttons of puzzle.
  135.     rem
  136.  
  137.     for x=1 to topx
  138.       for y=1 to topy
  139.     num=board(x,y)
  140.     if num<>0 then
  141.       CBUTTON text$(num-59),num+1000,0,"Push",0,(x*3)+boardleft,y+boardtop,3,1,buttonfore,boardc(x,y)
  142.     end if
  143.       next y
  144.     next x
  145.  
  146.  
  147. rem
  148. rem start of main program input loop
  149. rem
  150.  
  151. 100
  152.     a$=inkey$
  153.     if a$="" then goto 100
  154.     if len(a$)=1 then goto 100
  155.  
  156.     rem
  157.     rem get here if extended keycode returned
  158.     rem
  159.     num=asc(right$(a$,1))
  160.  
  161.     if num=68 then
  162.       rem
  163.       rem 1068 is what we defined our exit button to be
  164.       rem
  165.       stop
  166.  
  167.     elseif num=67 then
  168.       rem
  169.       rem help button pressed if here
  170.       rem
  171.       a$="Basic is your best value"
  172.       ll=len(a$)
  173.       a$=space$(totalx-1)+a$
  174.       ll=len(a$)
  175.       color borderfore,borderback
  176.       for i=1 to ll
  177.     locate boardtop+5,boardleft-1,0
  178.     b$=left$(a$,totalx)
  179.     print b$;
  180.     l=len(a$)
  181.     a$=right$(a$,l-1)+" "
  182.     t=timer
  183.        t100:
  184.     if timer-t<.05 then goto t100
  185.       next i
  186.       locate boardtop+5,boardleft-1,0
  187.       print space$(11);
  188.       goto 100
  189.  
  190.     end if
  191.  
  192.     if num<59 or num>66 then goto 100
  193.  
  194.     rem
  195.     rem If we get here then one of our puzzle buttons pressed
  196.     rem
  197.     rem button keycode selected is in num
  198.     rem find which square contains this button
  199.     rem
  200.       bx=0
  201.       by=0
  202.       for x=1 to topx
  203.     for y=1 to topy
  204.       if board(x,y)=num then bx=x:by=y
  205.     next y
  206.       next x
  207.       if bx=0 then beep:goto 100: rem oops, something wrong if can't find key
  208.  
  209.       rem
  210.       rem see if adjacent square is empty
  211.       rem
  212.       for x=-1 to 1 step 1
  213.     for y=-1 to 1 step 1
  214.       if abs(x)<>abs(y) then
  215.         if board(bx+x,by+y)=0 then goto gotempty
  216.       end if
  217.     next y
  218.       next x
  219.  
  220.       beep     : rem no empty square around this one
  221.       goto 100
  222.  
  223.  
  224. rem
  225. rem have empty square move to it
  226. rem
  227.  
  228. gotempty:
  229.  
  230.   dbutton num+1000
  231.   board(bx,by)=0
  232.   board(bx+x,by+y)=num
  233.   newx=(bx+x)*3
  234.   newy=(by+y)
  235.   CBUTTON text$(num-59),num+1000,0,"Push",0,newx+boardleft,newy+boardtop,3,1,buttonfore,boardc(bx+x,by+y)
  236.  
  237.      goto 100
  238.  
  239.